package com.jinglvhuanjing.app.helper.statusbar;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.jinglvhuanjing.app.utils.ScreenUtils;


public class StatusBarView extends View {

    private int mStatusBarHeight;
    private int mCurrentWidth;
    private int mCurrentHeight;
    private boolean isEnableImmerseStatusBar = true;

    public StatusBarView(Context context) {
        super(context);
        this.initViews(context);
    }

    public StatusBarView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.initViews(context);
    }

    public StatusBarView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.initViews(context);
    }

    private void initViews(Context context) {
        this.mStatusBarHeight = ScreenUtils.getStatusBarHeight(context);
    }

    public void setEnableImmerseStatusBar(boolean enableImmerse) {
        this.isEnableImmerseStatusBar = enableImmerse;
        if (isEnableImmerseStatusBar && ScreenUtils.isSupportImmerseStatusBar()) {
            setMeasuredDimension(mCurrentWidth, mStatusBarHeight);
        } else {
            setMeasuredDimension(mCurrentWidth, 0);
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        final int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
        final int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
        mCurrentWidth = MeasureSpec.getSize(widthMeasureSpec);
        mCurrentHeight = MeasureSpec.getSize(heightMeasureSpec);
        if (isEnableImmerseStatusBar && ScreenUtils.isSupportImmerseStatusBar()) {
            setMeasuredDimension(mCurrentWidth, mStatusBarHeight);
        } else {
            setMeasuredDimension(mCurrentWidth, 0);
        }
    }

    /**
     * 附加方法：获取当前状态栏的高度
     */
    public int getStatusBarHeight() {
        return ScreenUtils.getStatusBarHeight(getContext());
    }
}